/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.gateway.server.client;

import com.iwindplus.auth.domain.constant.AuthConstant;
import com.iwindplus.base.domain.constant.CommonConstant.NetWorkConstant;
import com.iwindplus.base.domain.vo.ResultVO;
import com.iwindplus.base.domain.vo.UserBaseVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * auth 客户端.
 *
 * @author zengdegui
 * @since 2025/08/24 16:18
 */
@Slf4j
@Component
public class AuthClient {

    public static final String AUTH_PREFIX = String.format("%s%s", NetWorkConstant.LB_PREFIX, AuthConstant.AUTH_SERVER_NAME);

    @Resource
    private WebClient loadBalancedWebClient;

    /**
     * 获取用户信息.
     *
     * @param accessToken 访问token
     * @return Mono<UserBaseVO>
     */
    public Mono<UserBaseVO> checkAccessToken(String accessToken) {
        final String url = String.format("%s%s?accessToken={accessToken}", AUTH_PREFIX,
            "/inner/authorization/checkAccessToken");

        return loadBalancedWebClient.get()
            .uri(url, accessToken)
            .retrieve()
            .bodyToMono(new ParameterizedTypeReference<ResultVO<UserBaseVO>>() {
            }).flatMap(ResultVO::unwrap);
    }

}
